﻿using IService.Blog;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace BlogAPI.Controllers
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    //[AllowAnonymous]
    public class MenusController : ControllerBase
    {
        readonly ILogger<MenusController> logger;
        readonly IMenusService menusService;
        public MenusController(ILogger<MenusController> _logger, IMenusService _menusService)
        {
            logger = _logger;
            menusService = _menusService;
        }


        /// <summary>
        /// 查询所有菜单（分页）
        /// </summary>
        /// <param name="offset">页数</param>
        /// <param name="limit">每页数</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有菜单")]
        public async Task<MessageModel<PageInfoModel<BMenu>>> GetAllToPage(int offset = 1, int limit = 99)
        {
            try
            {
                return await menusService.GetAllToPage(offset, limit);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<PageInfoModel<BMenu>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 按角色ID查询所有菜单
        /// </summary>
        /// <param name="offset">页数</param>
        /// <param name="limit">每页数</param>
        /// <param name="roleID">角色ID</param>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有菜单")]
        public async Task<MessageModel<List<BMenu>>> GetAllToPageByRoleID(int roleID=1)
        {
            try
            {
                return await menusService.GetAllToPageByRoleID(roleID);
            }
            catch (Exception ex)
            {
                logger.LogError(ex.Message);
                return new MessageModel<List<BMenu>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }
        }


        /// <summary>
        /// 查询所有菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("查询所有菜单")]
        public async Task<MessageModel<List<BMenu>>> GetAll()
        {
            try
            {
                return await menusService.GetAll();
            }
            catch (Exception ex)
            {
                return new MessageModel<List<BMenu>>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 按ID查询菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("按ID查询菜单")]
        public async Task<MessageModel<BMenu>> GetById(int id)
        {
            try
            {
                return await menusService.GetById(id);
            }
            catch (Exception ex)
            {
                return new MessageModel<BMenu>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }


        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("添加菜单")]
        public async Task<MessageModel<string>> Post([FromBody] BMenu parameter)
        {
            try
            {
                return await menusService.Post(parameter);
            }
            catch (Exception ex)
            {
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        [HttpPut]
        [Description("修改菜单")]
        public async Task<MessageModel<string>> Put([FromBody] BMenu parameter)
        {
            try
            {
                return await menusService.Put(parameter);
            }
            catch (Exception ex)
            {
                return new MessageModel<string>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Description("删除菜单")]
        public async Task<MessageModel<bool>> DelById(int id)
        {
            try
            {
                return await menusService.DelById(id);
            }
            catch (Exception ex)
            {
                return new MessageModel<bool>()
                {
                    Message = "服务异常" + ex.Message,
                };
            }

        }
    }
}
